<?php

namespace App\Service;

use App\Models\EwPush;

class PushService extends BaseService
{
	public static function getList($params, $count = FALSE)
	{
		$query = EwPush::query();
		$query->columns(['id,to_member_id,title,go_url,logo_image,content,create_time']);

		//通过内容
		if (isset($params->content) && $params->content)
			$query->andwhere("content LIKE '%" . $params->content . "%'");

		//通过标题
		if (isset($params->title) && $params->title)
			$query->andwhere("title LIKE '%" . $params->title . "%'");

		//总条数
		if ($count)
			return $query->execute()->count();

		//order_by 排序
		if (isset($params->order_by)){
			$sql = parent::orderBy($params->order_by);
			$query->orderBy($sql);
		}

		//分页
		$query->limit($params->page_num,($params->p-1)*$params->page_num);

		return $query->execute();
	}

	public static function getInfo($id = 0)
	{
		return EwPush::findFirst($id);
	}

	public static function count()
	{
		return EwPush::count();
	}

	/**
	 * [save 保存数据]
	 * @param  [type] $data [description]
	 * @return [type]       [description]
	 */
	public static function save($data)
	{
		$row = new EwPush;
		if (isset($data->id) && $data->id)
			$row = EwPush::findFirst($data->id);

		return $row->save((array)$data);
	}
}